//
//  ScheduleVC.m
//  NJUSTHelper
//
//  Created by 郑越辉 on 2017/9/14.
//  Copyright © 2017年 njust. All rights reserved.
//

#import "ScheduleVC.h"
#import "MoreScheduleVC.h"
#import <PopoverView.h>
#import "UserDBService.h"
#import "ScheduleDBService.h"
#import "ScheduleModel.h"
#import "UserModel.h"
@interface ScheduleVC ()
@property (nonatomic, strong) UIBarButtonItem *rightBarItem;
@property (nonatomic, strong) UIButton *btn;

@property (weak, nonatomic) IBOutlet UIView *bottomView;


@end

@implementation ScheduleVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"课程表";
    
    
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc]initWithTitle:@"按周查看" style:UIBarButtonItemStylePlain target:self action:@selector(moreSchedule)];
    [self.navigationItem setLeftBarButtonItem:leftBarItem];
    self.rightBarItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"schedule_more_items"] style:UIBarButtonItemStylePlain target:self action:@selector(popMenuAction:)];
    [self.navigationItem setRightBarButtonItem:self.rightBarItem];
    
    if(AccountInfo.isLogin == NO) {
        LoginAlertView *alert = [[LoginAlertView alloc]init];
        alert.okActionBlock = ^{
            [self loginAction];
        };
        [alert show];
    }
    
    
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    
    self.bottomHeight = self.bottomView.frame.size.height;
    [self showSchedule];
    
}

- (void)didSelectSegmentIndex:(NSInteger)index
{
    NSLog(@"%ld", (long)index);
}

-(void)loginAction{
    
    [self presentLoginVCAction];
}

-(void)moreSchedule{
    
    MoreScheduleVC *vc = [[MoreScheduleVC alloc]init];
    
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)popMenuAction:(id)sender{
    PopoverView *popoverView = [PopoverView popoverView];
    popoverView.showShade = YES; // 显示阴影背景
//    [popoverView showToPoint:CGPointMake(self.view.width, self.navigationController.navigationBar.frame.size.height + [[UIApplication sharedApplication] statusBarFrame].size.height) withActions:[self popItems]];
    [popoverView showToPoint:CGPointMake(self.view.width, self.navigationController.navigationBar.frame.size.height) withActions:[self popItems]];
}

-(NSArray<PopoverAction *> *)popItems{
    PopoverAction *action1 = [PopoverAction actionWithImage:[UIImage imageNamed:@"schedule_right_import"] title:@"教务处导入" handler:^(PopoverAction *action) {
        [self importScheduleFromNet];
    }];
    PopoverAction *action2 = [PopoverAction actionWithImage:[UIImage imageNamed:@"schedule_right_clear"] title:@"清空课表" handler:^(PopoverAction *action) {
        
    }];
//    PopoverAction *action3 = [PopoverAction actionWithImage:[UIImage imageNamed:@"schedule_left_clear"] title:@"修改课表" handler:^(PopoverAction *action) {
//        
//    }];

    PopoverAction *action4 = [PopoverAction actionWithImage:[UIImage imageNamed:@"schedule_right_switch"] title:@"切换用户" handler:^(PopoverAction *action) {
        [self loginAction];
    }];
    PopoverAction *action5 = [PopoverAction actionWithImage:[UIImage imageNamed:@"schedule_right_logout"] title:@"退出登录" handler:^(PopoverAction *action) {
        
    }];
    PopoverAction *action6 = [PopoverAction actionWithImage:[UIImage imageNamed:@"schedule_right_switch"] title:@"登录" handler:^(PopoverAction *action) {
        [self loginAction];
    }];
    if(AccountInfo.isLogin){
        return @[action1, action2, action4, action5];
    }else{
        return @[action6];
    }
}

-(void)importScheduleFromNet{
    [SVProgressHUD show];
    
    if(![HtmlAnalysisHelper analysiScheduleAndSave]){
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:@"导入失败,请稍后重试"];
        return;
    }
    
    [self reloadEveryDaySchedule];
    
    [SVProgressHUD dismiss];
    [SVProgressHUD showSuccessWithStatus:@"导入成功"];
    
    
}
-(void)logoutUserAction{
    [self logoutAction];
    [self reloadEveryDaySchedule];
}


@end
